package com.hollycrm.ahunicom.sheet.business.logic.hibernate;

import java.util.Iterator;
import java.util.List;

import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.hollycrm.unicom.security.AuthToken;
import com.hollycrm.unicom.sheet.Constants;
import com.hollycrm.unicom.sheet.domain.ConfirmSheet;
import com.hollycrm.unicom.sheet.domain.DealSheet;
import com.hollycrm.unicom.sheet.domain.MainSheet;
import com.hollycrm.unicom.sheet.domain.SheetAppu;
import com.hollycrm.unicom.sheet.flow.SheetAction;
import com.hollycrm.unicom.sheet.flowsupport.ActionExecuteLogic;
import com.hollycrm.unicom.sheet.flowsupport.DoOperateContext;

public class HibernateDeleteAppuLogic extends HibernateDaoSupport implements
		ActionExecuteLogic {
    public HibernateDeleteAppuLogic() {
    }

    public void execute(AuthToken authToken,SheetAction action, DoOperateContext context) throws
        Exception {
        HibernateTemplate ht = getHibernateTemplate();
        String formFileName = (String) context.getInputAttr("formFileName");
        String[] rowIDs = (String[])context.getInputAttr("rowIDs");
        String[] savedNames = new String[rowIDs.length];
        String sheetFlowNO = null;
        Object sheet = null;
        for(int i=0;i<rowIDs.length;i++){
        SheetAppu appu=(SheetAppu)ht.load(SheetAppu.class,rowIDs[i]);
        updateAppuCount(ht,appu);
        if(i==0)
            sheetFlowNO = appu.getSheetFlowNO();
        savedNames[i]=appu.getSavedName();
        ht.delete(appu);
        }

        context.addOutputAttr("dealType","DELETE");
        context.addOutputAttr("savedNames",savedNames);
        context.addOutputAttr("sheetFlowNO",sheetFlowNO);
//        context.addOutputAttr("mainSheetFlowNO", mainSheetFlowNO);
//        context.addOutputAttr("operationObject", appu.getSheetFlowNO());
    }

    private String updateAppuCount(HibernateTemplate ht,
                                   SheetAppu appu) throws Exception {
        String flowNO = null;
        if (appu.getSheetType().equals(Constants.SHEET_TYPE_MAIN)) {
            MainSheet sheet = (MainSheet) ht.load(MainSheet.class,
                                                  appu.getSheetFlowNO());
            flowNO = sheet.getSheetFlowNO();
            sheet.setAppuCount(new Integer(sheet.getAppuCount().intValue() - 1));
            ht.update(sheet);
        }
        else if (appu.getSheetType().equals(Constants.SHEET_TYPE_DEAL)) {
            MainSheet sheet = (MainSheet)ht.load(MainSheet.class,appu.getSheetFlowNO());
            flowNO = sheet.getSheetFlowNO();
            sheet.setAppuCount(new Integer(sheet.getAppuCount().intValue()-1));
            ht.update(sheet);
            
            List dealSheetList = ht.find("from dealSheet in " + DealSheet.class + " where dealSheet.mainSheetFlowNO='" + flowNO + "'");
            Iterator it = dealSheetList.iterator();
            while(it.hasNext()) {
            	DealSheet dealSheet = (DealSheet)it.next();
        	    flowNO = dealSheet.getMainSheetFlowNO();
        	    dealSheet.setAppuCount(new Integer(sheet.getAppuCount().intValue()));
        	    ht.update(dealSheet);
            }
        }
        else if (appu.getSheetType().equals(Constants.SHEET_TYPE_CONFIRM)) {
            ConfirmSheet sheet = (ConfirmSheet) ht.load(ConfirmSheet.class,
                appu.getSheetFlowNO());
            flowNO = sheet.getMainSheetFlowNO();
            sheet.setAppuCount(new Integer(sheet.getAppuCount().intValue() - 1));
            ht.update(sheet);
        }
        return flowNO;
    }
}
